<?php

App::import('Security');

class AppController extends Controller {
    
    var $components = array('Acl','Auth', 'RequestHandler','Session','Upload',"Cookie");
    var $paginate = array('limit' => 10);
    var $uses = array('Setting','User','Blockarea','Category','Post','District','Page');
    var $helpers = array('Html', 'Form', 'Javascript','Cache','Session','Menu','Common','Block','Text');
    var $blocks;
    
    //var $site_title ='DropNCms - Admin Panel';
    var $site_title ='Chikitsha.com - Content Management System';
    var $site_title_body ='Welcome to control panel. Here you can mange site contents.';
    
    var $front_page_title ='';
    
    var $_settings = array();
    
    var $_filtered=false;
    
    
    var $lang_prefixes=array("eng"=>"","ben"=>"bangla_");
    
    var $cur_lang="";
    
    
    function beforeFilter() {
        
        
        if(isset($this->params['url']["toggle_language"])){
     //       echo $this->Session->read('Config.language');
            if($this->Session->read('Config.language')=="ben"){
                $this->Session->write('Config.language', "eng");
                $this->Cookie->write('lang', "eng", false, '20 days');
            }
            else {
                $this->Session->write('Config.language', "ben");
                $this->Cookie->write('lang', "ben", false, '20 days');
            }
            //     die;
            
            $this->redirect($this->referer());
            exit;
        }
        
        $this->_setLanguage();
        
        $this->cur_lang=$this->lang_prefixes[$this->Session->read('Config.language')];
        //echo $this->cur_lang;
        $this->set("lang_prefix",$this->cur_lang);
        
        $_filtered=true;
        
        // retriveing cms settings
        $this->_settings = $this->Setting->find('list', array('fields' => array('key', 'value')));
        
        
        $this->set($this->_settings);
        $this->set('site_settings',$this->_settings);
        
        
        // end of retriveing
        
        
        // Configure AuthComponent
        Security::setHash('md5'); 
        $this->Auth->autoRedirect = false;
        $this->Auth->authorize = 'actions';
        $this->Auth->actionPath = 'controllers/';
        $this->Auth->logoutRedirect = array('/');
        
        // public actions
        $this->Auth->allowedActions = array(
            'display',
            'index',
            'login',
            'logout',
            'recover',
            'activate',
            'register',
            'details',
            'categories',
            'local',
            'localities',
            'ajax_districts',
            'add',
            'search',
            'archive',
            'clean',
        );
        
        $this->Auth->userScope = array('User.status' => 'active');
        
        // allowing all actions in development mode
        
        if(Configure::read('debug')==2 || !$this->Auth->user())
            $this->Auth->allow('*');
        
        // end of configuration
        
            
    

        // detecting admin
        $admin = Configure::read('Routing.admin');
        
        
        if($admin && isset($this->params[$admin])) :
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => true);            
            $this->Auth->loginRedirect = array('controller' => 'dashboards', 'action' => 'index', 'admin' => true);
            $this->paginate['limit'] = $this->_settings['admin_page_factor'];    
            
            
            if($this->Auth->user('group_id')!=1 && ($this->params['url']['url']!='cp') && !isset($this->data)):
                header("HTTP/1.0 404 Not Found");
                exit;
            endif;                
            
            
            if($this->Auth->user())
                $this->layout = 'admin/admin';
            else $this->layout = 'admin/login';
            
        else:
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
            //die('<h2>under construction<h2>');
            $this->paginate['limit'] = $this->_settings['public_page_factor'];
            $this->layout = 'public/default';
            
        endif;
        // end of detection
        
        
        $ajax = Configure::read('Routing.ajax');
        if($ajax && isset($this->params[$ajax])) :
            Configure::write('debug', 0);
            $this->layout = 'public/ajax';
        endif;
        
        // flashing message
        if($this->Session->read('Success'))$flash_type='success';
        else $flash_type='error';
        
        $this->set(compact('flash_type'));
        $this->Session->write('Success',false);
        // end of flasing message
        
        
        
        
        
    }
    
    function beforeRender() {
        $notices=$this->Post->find("all",array("recursive"=>-1,"conditions"=>array("category_id"=>9998)));
        $plinks=$this->Post->find("all",array("recursive"=>-1,"conditions"=>array("additional"=>"for patients","category_id"=>9999)));
        $dlinks=$this->Post->find("all",array("recursive"=>-1,"conditions"=>array("additional"=>"for doctors","category_id"=>9999)));
        
        $this->set(compact("notices"));
        $this->set(compact("plinks"));
        $this->set(compact("dlinks"));    
        
        // session user data for view
        $this->set('user', $this->Auth->user());
        
        $this->set('site_title',$this->site_title);
        $this->set('front_page_title',$this->front_page_title);
        $this->set('site_title_body',$this->site_title_body);
        
        // generate admin menu

        $admin = Configure::read('Routing.admin');

        if($this->Auth->user() && ($admin && isset($this->params[$admin]))){
            
            if(($menus = Cache::read('admin_menu-'.$this->Auth->user('group_id'), 'default')) === false) :
                $group =&$this->User->Group;
                $group->id = $this->Auth->user('group_id');
            
                $menus = array(
                    'menu-dashboard' => 
                        array(
                            'title' => 'Dashboard',
                            'url' => array('controller' => 'dashboards', 'action' => 'index'),
                            'access'=>$this->Acl->check($group,'controllers/Dashboards/admin_index'),
                            
                        ),
                    
                    'menu-news' => 
                        array(
                            'title' => 'Site Data',
                            'url' => array('controller' => 'doctors', 'action' => 'index'),
                            'access'=>true,
                            'submenu'=>array(
                                array(
                                    'title' =>'Manage Doctors',
                                    'url' => array('controller' => 'doctors', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'doctors','action'=>'add'),
                                        array('controller'=>'doctors','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Manage Hospitals',
                                    'url' => array('controller' => 'hospitals', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'hospitals','action'=>'add'),
                                        array('controller'=>'hospitals','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Manage Ambulances',
                                    'url' => array('controller' => 'ambulances', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'ambulances','action'=>'add'),
                                        array('controller'=>'ambulances','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Manage Blood Banks',
                                    'url' => array('controller' => 'bloods', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'bloods','action'=>'add'),
                                        array('controller'=>'bloods','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Diagnostic Centers',
                                    'url' => array('controller' => 'diagnostics', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'diagnostics','action'=>'add'),
                                        array('controller'=>'diagnostics','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Medical Equipments',
                                    'url' => array('controller' => 'equipments', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'equipments','action'=>'add'),
                                        array('controller'=>'equipments','action'=>'edit'),
                                    )
                                ),
                                array(
                                    'title' =>'Medicine Stores',
                                    'url' => array('controller' => 'medicines', 'action' => 'index'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'medicines','action'=>'add'),
                                        array('controller'=>'medicines','action'=>'edit'),
                                    )
                                ),
                                
                            )
                        ),  
                    
                    'menu-sitecontent' => 
                        array(
                            'title'=>"Contents",
                            "url"=>array("controller"=>"districts","action"=>"index"),
                            "access"=>true,
                            "submenu"=>array(
                                array(
                                    'title' =>'Locations',
                                    'url' => array('controller' => 'districts', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/districts/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'districts','action'=>'add'),
                                        array('controller'=>'districts','action'=>'edit'),
                                        array('controller'=>'districts','action'=>'tree'),
                                        
                                    )
                                    
                                    
                                    
                                ),
                               
                               
                                 /*array(
                                    'title' =>'Category',
                                    'url' => array('controller' => 'categories', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/Categories/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'categories','action'=>'add'),
                                        array('controller'=>'categories','action'=>'edit'),
                                        array('controller'=>'categories','action'=>'tree'),
                                        
                                    )
                                    
                                    
                                    
                                ),*/
                               array(
                                    'title' =>'Pages',
                                    'url' => array('controller' => 'pages', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/pages/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'pages','action'=>'add'),
                                        array('controller'=>'pages','action'=>'edit'),
                                        
                                    )
                                    
                                    
                                    
                                ),
                               array(
                                    'title' =>'Home Page Banners',
                                    'url' => array('controller' => 'media', 'action' => 'index',"homebanner",1),
                                    'access'=>$this->Acl->check($group,'controllers/media/index'),
                                    'alias'=>array(
                                        array('/admin/media/index/homebanner/'),
                                        
                                        
                                    )
                                    
                                    
                                    
                                ),
                               array(
                                    'title' =>'Links',
                                    'url' => array('controller' => 'links', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/links/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'links','action'=>'add'),
                                        array('controller'=>'links','action'=>'edit'),
                                        
                                    )
                                    
                                    
                                    
                                ),
                               
                               array(
                                    'title' =>'Notices',
                                    'url' => array('controller' => 'notices', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/notices/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'notices','action'=>'add'),
                                        array('controller'=>'notices','action'=>'edit'),
                                        
                                    )
                                    
                                    
                                    
                                ),
                               
                               
                               
                            )
                        ),
                    'menu-attr' => 
                        array(
                            'title' => 'Attributes',
                            'url' => array('controller' => 'doctors', 'action' => 'degree'),
                            'access'=>true,
                            'submenu'=>array(
                                array(
                                    'title' =>'Doctors Degree',
                                    'url' => array('controller' => 'doctors', 'action' => 'degree'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'doctors','action'=>'add_degree'),
                                        array('controller'=>'doctors','action'=>'edit_degree'),
                                        
                                    )
                                ),
                                array(
                                    'title' =>'Doctors Specialization',
                                    'url' => array('controller' => 'doctors', 'action' => 'spec'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'doctors','action'=>'add_spec'),
                                        array('controller'=>'doctors','action'=>'edit_spec'),
                                        
                                    )
                                ),
                                array(
                                    'title' =>'Diagnostic Center Services',
                                    'url' => array('controller' => 'diagnostics', 'action' => 'service'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('controller'=>'diagnostics','action'=>'add_service'),
                                        array('controller'=>'diagnostics','action'=>'edit_service'),
                                        
                                    )
                                ),
                                
                            )
                        ),  
                    
                    
                    'menu-blocks' => 
                        array(
                            'title' => 'Manage Advertises',
                            'url' => array('controller' => 'blocks', 'action' => 'index'),
                            'access'=>$this->Acl->check($group,'controllers/Blocks/admin_index'),
                            'submenu'=>array(
                                array(
                                    'title' =>'Block Area',
                                    'url' => array('controller' => 'blocks', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/Blocks/admin_index'),
                                    
                                    
                                    
                                ),
                                array(
                                    'title' =>'Blocks',
                                    'url' => array('controller' => 'blocks', 'action' => 'manage'),
                                    'access'=>$this->Acl->check($group,'controllers/Blocks/admin_manage'),
                                    
                                    
                                    
                                ),
                                
                            )
                        ),  
                      'menu-users' => 
                        array(
                            'title' => 'Users',
                            'url' => array('controller' => 'users', 'action' => 'index'),
                            'access'=>$this->Acl->check($group,'controllers/Users/admin_index'),
                            'submenu'=>array(
                                array(
                                    'title' =>'Public Users',
                                    'url' => array('controller' => 'users', 'action' => 'public','hold'),
                                    'access'=>$this->Acl->check($group,'controllers/Users/admin_public'),
                                    'alias'=>array(
                                        array('controller'=>'users','action'=>'public'),
                                        array('controller'=>'users','action'=>'public','banned'),
                                    )
                                ),
                                array(
                                    'title' =>'Administrators',
                                    'url' => array('controller' => 'users', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/Users/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'users','action'=>'add'),
                                        array('controller'=>'users','action'=>'edit'),
                                        
                                    )
                                ),
                                
                                array(
                                    'title' =>'Profile',
                                    'url' => array('controller' => 'users', 'action' => 'profile'),
                                    'access'=>true,
                                    'alias'=>array(
                                        array('/admin/users/profile'),
                                        
                                        
                                    )
                                    
                                ),
                                
                            )
                            
                    ),
                    'menu-settings' => 
                        array(
                            'title' => 'Settings',
                            'url' => array('controller' => 'dashboards', 'action' => 'settings'),
                            'access'=>$this->Acl->check($group,'controllers/Dashboards/admin_settings'),
                            'submenu'=>array(
                                array(
                                    'title' =>'Site Settings',
                                    'url' => array('controller' => 'dashboards', 'action' => 'settings'),
                                    'access'=>$this->Acl->check($group,'controllers/Dashboards/admin_settings'),
                                    
                                ),
                                array(
                                    'title' =>'Groups',
                                    'url' => array('controller' => 'groups', 'action' => 'index'),
                                    'access'=>$this->Acl->check($group,'controllers/Groups/admin_index'),
                                    'alias'=>array(
                                        array('controller'=>'groups','action'=>'add'),
                                        array('controller'=>'groups','action'=>'edit'),
                                        array('controller'=>'users','action'=>'permissions'),
                                    )
                                           
                                ),
                            )
                            
                    ),
                    
                );
                
                   $bigButtons='<ul class="controls">';
                   
                   $bigButtons.='<li> <a href="'.Router::url('/admin/pages').'"><b class="mail">Pages</b></a> </li>';
                   
                   $bigButtons.='<li> <a href="'.Router::url('/admin/media/index/homebanner/1').'"><b class="box">Home Banners</b></a> </li>';
                   
                   $bigButtons.='<li> <a href="'.Router::url('/admin/links').'"><b class="clipboard">Links</b></a> </li>';
                   
                   $bigButtons.='<li> <a href="'.Router::url('/admin/notices').'"><b class="display">Notices</b></a> </li>';
                        
                   if($this->Acl->check($group,'controllers/Blocks/admin_manage'))
                        $bigButtons.='<li> <a href="'.Router::url('/admin/blocks/manage').'"><b class="box2">Advertises</b></a> </li>';
                   
                   if($this->Acl->check($group,'controllers/Users/admin_index'))
                        $bigButtons.='<li> <a href="'.Router::url('/admin/users').'"><b class="briefcase">Users</b></a> </li>';
                            
                   if($this->Acl->check($group,'controllers/Dashboards/admin_settings'))
                        $bigButtons.='<li> <a href="'.Router::url('/admin/settings').'"><b class="docs">Settings</b></a> </li>';
                            
                   /*if($this->Acl->check($group,'controllers/Groups/admin_index'))
                        $bigButtons.='<li> <a href="'.Router::url('/admin/groups').'"><b class="display">User Groups</b></a> </li>';
                   */         
                        
                   $bigButtons.='</ul>';
                        
                   
                    $menus['menus']=$menus;
                    $menus['bigButtons']=$bigButtons;
                    
                    Cache::write('admin_menu-'.$this->Auth->user('group_id'), $menus, 'default');
                endif;
            
            
                
            $this->set('bigButtons',$menus['bigButtons']);
            $this->set('menus',$menus['menus']);
        }
        elseif(!isset($this->params[$admin])) {
            $this->__public_menu();
        }
        
    }   
    
    
    function __build_page_array($cats){
        if(count($cats))
            $output=array();
            foreach($cats as $c){
                $output['menu-'.$c['Page']['id']]['title']=($c['Page'][$this->cur_lang.'title']?$c['Page'][$this->cur_lang.'title']:$c['Page']['title']);
                $output['menu-'.$c['Page']['id']]['url']=($c['Page']['link']?$c['Page']['link']:'/'.$c['Page']['name'].'.html');
                $output['menu-'.$c['Page']['id']]['access']=true;
                
                if(count($c['children'])){
                    $output['menu-'.$c['Page']['id']]['submenu']=$this->__build_page_array($c['children']);
                }
            }
            return $output;
        }
        
    
    
    function __public_menu(){
        
        
            
        /*if(($front_menu = Cache::read('front_menu', 'default')) === false) :*/
            $pages=array();
            $p=$this->Page->find('threaded',array('recursive'=>-1,'conditions'=>array('in_header'=>1),'order' => 'lft ASC'));
            
            $pages=$this->__build_page_array($p);
            
            $footer=$this->Page->find('all',array('recursive'=>-1,'conditions'=>array('in_footer'=>1),'order' => 'lft ASC'));
            
            $front_menu['main_menu']=$pages;
            $front_menu['footer_menu']=$footer;
            
        /*    Cache::write('front_menu', $front_menu, 'default');
        endif;    */
        
        extract($front_menu);
        
        
        //pr($main_menu);
        $this->set('public_menu',$main_menu);
        $this->set('footer_menu',$footer_menu);
        
        
    }
    
    
    function _setLanguage() {
        if ($this->Cookie->read('lang')) {
            $this->Session->write('Config.language', $this->Cookie->read('lang'));
            Configure::write("Config.language",$this->Cookie->read('lang'));
        }
        else {
            
            $this->Session->write('Config.language', "ben");
            $this->Cookie->write('lang', "ben", false, '20 days');
        }
    }
    
    
    
    
    
    
    
    /**
     * Returns an indented html select based on children depth
     *
     * @param array $data_array - Array of data passed in from cake's find('threaded') feature
     * @param array $list - Used internally, contains array to be returned
     * @param int $counter - Used Internally, counter for depth
     * @return array
     */
    public function drop_down_from_threaded($data_array,$select_id,$select_label,$model, &$list = array(), $counter = 0)
    {
        if ( ! is_array($data_array))
            return array();
            
        
        foreach ($data_array AS $data)
        {
            $list[$data[$model][$select_id]] = str_repeat('-', $counter).$data[$model][$select_label];
            if ( ! empty($data['children']))
            {
                $this->drop_down_from_threaded($data['children'],$select_id,$select_label,$model, $list, $counter + 1);
            }
        }
        return $list;
    }
    
    
}
?>
